using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Resources;
using System.Windows.Forms;
using FlexCell;
using Infragistics.Win.UltraWinTabControl;
using Infragistics.Win.UltraWinTabs;
using PluginFrame;

namespace PcrExperimentProject
{
	public class AbsoluteAnalysisParameters : Form
	{
		private IContainer components;

		protected Panel panel1;

		protected Button btnConfirm;

		protected Button btnCancel;

		protected CheckBox chkAutoAnalysis;

		protected ResourceManager mResourceManager;

		protected Dictionary<string, bool> mIsOptionChanged;

		protected List<IPcrExperimentProject> mPcrExerimentProjects;

		protected UltraTabControl mOptionsTabControl;

		protected List<IAnalysisParameterConfiguration> mAnalysisParameterConfigurationList;

		protected IConfigurationService mConfigurationSerivce;

		protected override void Dispose(bool disposing)
		{
			if (disposing && components != null)
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}

		protected void InitializeComponent()
		{
			System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(PcrExperimentProject.AbsoluteAnalysisParameters));
			this.panel1 = new System.Windows.Forms.Panel();
			this.chkAutoAnalysis = new System.Windows.Forms.CheckBox();
			this.btnConfirm = new System.Windows.Forms.Button();
			this.btnCancel = new System.Windows.Forms.Button();
			this.panel1.SuspendLayout();
			base.SuspendLayout();
			this.panel1.AccessibleDescription = null;
			this.panel1.AccessibleName = null;
			resources.ApplyResources(this.panel1, "panel1");
			this.panel1.BackgroundImage = null;
			this.panel1.Controls.Add(this.chkAutoAnalysis);
			this.panel1.Controls.Add(this.btnConfirm);
			this.panel1.Controls.Add(this.btnCancel);
			this.panel1.Font = null;
			this.panel1.Name = "panel1";
			this.chkAutoAnalysis.AccessibleDescription = null;
			this.chkAutoAnalysis.AccessibleName = null;
			resources.ApplyResources(this.chkAutoAnalysis, "chkAutoAnalysis");
			this.chkAutoAnalysis.BackgroundImage = null;
			this.chkAutoAnalysis.Font = null;
			this.chkAutoAnalysis.Name = "chkAutoAnalysis";
			this.chkAutoAnalysis.UseVisualStyleBackColor = true;
			this.btnConfirm.AccessibleDescription = null;
			this.btnConfirm.AccessibleName = null;
			resources.ApplyResources(this.btnConfirm, "btnConfirm");
			this.btnConfirm.BackgroundImage = null;
			this.btnConfirm.Font = null;
			this.btnConfirm.Name = "btnConfirm";
			this.btnConfirm.UseVisualStyleBackColor = true;
			this.btnConfirm.Click += new System.EventHandler(btnConfirm_Click);
			this.btnCancel.AccessibleDescription = null;
			this.btnCancel.AccessibleName = null;
			resources.ApplyResources(this.btnCancel, "btnCancel");
			this.btnCancel.BackgroundImage = null;
			this.btnCancel.Font = null;
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.UseVisualStyleBackColor = true;
			this.btnCancel.Click += new System.EventHandler(btnCancel_Click);
			base.AccessibleDescription = null;
			base.AccessibleName = null;
			resources.ApplyResources(this, "$this");
			base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.BackgroundImage = null;
			base.Controls.Add(this.panel1);
			this.Font = null;
			base.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			base.Icon = null;
			base.MaximizeBox = false;
			base.MinimizeBox = false;
			base.Name = "AbsoluteAnalysisParameters";
			base.ShowIcon = false;
			base.ShowInTaskbar = false;
			this.panel1.ResumeLayout(false);
			this.panel1.PerformLayout();
			base.ResumeLayout(false);
		}

		public AbsoluteAnalysisParameters(IApplication app, List<IPcrExperimentProject> pcrExerimentProjects, bool isShowAutoAnalysis)
		{
			InitializeComponent();
			mConfigurationSerivce = (IConfigurationService)app.GetService("ConfigurationService");
			mResourceManager = ResourceManager.CreateFileBasedResourceManager("PcrExperimentProject", app.GetResourceFolder() + GetType().Namespace + "\\", null);
			mPcrExerimentProjects = pcrExerimentProjects;
			mIsOptionChanged = new Dictionary<string, bool>();
			InitializeTab();
			btnConfirm.Text = mResourceManager.GetString("ConfirmString");
			btnCancel.Text = mResourceManager.GetString("CancelString");
			chkAutoAnalysis.Checked = isShowAutoAnalysis;
			chkAutoAnalysis.Visible = isShowAutoAnalysis;
		}

		protected void InitializeTab()
		{
			//IL_0122: Unknown result type (might be due to invalid IL or missing references)
			//IL_0128: Expected O, but got Unknown
			//IL_05a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_05b3: Expected O, but got Unknown
			mOptionsTabControl = new UltraTabControl();
			((ISupportInitialize)mOptionsTabControl).BeginInit();
			mOptionsTabControl.Dock = DockStyle.Fill;
			mOptionsTabControl.Name = "Options Tab Control";
			mOptionsTabControl.ViewStyle = ViewStyle.VisualStudio2005;
			mOptionsTabControl.Style = ((mPcrExerimentProjects.Count <= 1) ? UltraTabControlStyle.Wizard : UltraTabControlStyle.Default);
			mOptionsTabControl.TabLayoutStyle = TabLayoutStyle.SingleRowAutoSize;
			mOptionsTabControl.TabOrientation = TabOrientation.Default;
			mOptionsTabControl.ScrollButtons = TabScrollButtons.Always;
			mOptionsTabControl.AllowTabClosing = false;
			mOptionsTabControl.AllowTabMoving = false;
			((ISupportInitialize)mOptionsTabControl).EndInit();
			foreach (IPcrExperimentProject mPcrExerimentProject in mPcrExerimentProjects)
			{
				UltraTab ultraTab = new UltraTab();
				UltraTabPageControl ultraTabPageControl2 = (ultraTab.TabPage = new UltraTabPageControl());
				ultraTab.Text = mPcrExerimentProject.Name;
				ultraTab.Key = mPcrExerimentProject.Name;
				ultraTabPageControl2.Name = mPcrExerimentProject.Name;
				ultraTabPageControl2.Tag = mPcrExerimentProject.Name;
				mOptionsTabControl.Controls.Add(ultraTabPageControl2);
				mOptionsTabControl.Tabs.Add(ultraTab);
				Grid val = new Grid();
				((Control)(object)val).Dock = DockStyle.Fill;
				((Control)(object)val).BringToFront();
				val.ShowResizeTip = false;
				val.DisplayRowNumber = true;
				val.DisplayRowArrow = true;
				val.DisplayFocusRect = false;
				val.AllowUserResizing = (ResizeEnum)0;
				val.ExtendLastCol = false;
				val.ScrollBars = (ScrollBarsEnum)3;
				((Control)(object)val).Tag = mPcrExerimentProject;
				val.Cols = 9;
				val.Cell(0, 1).Text = mResourceManager.GetString("ChannelString");
				val.Column(1).CellType = (CellTypeEnum)0;
				val.Column(1).Locked = true;
				val.Column(1).CellType = (CellTypeEnum)0;
				val.Cell(0, 2).Text = mResourceManager.GetString("StartBaselineString");
				val.Column(2).CellType = (CellTypeEnum)0;
				val.Column(2).Mask = (MaskEnum)2;
				val.Column(2).MaxLength = 2;
				val.Cell(0, 3).Text = mResourceManager.GetString("StopBaselineString");
				val.Column(3).CellType = (CellTypeEnum)0;
				val.Column(3).Mask = (MaskEnum)2;
				val.Column(3).MaxLength = 2;
				val.Column(3).DecimalLength = 1;
				val.Cell(0, 4).Text = mResourceManager.GetString("AutoThresholdString");
				val.Column(4).CellType = (CellTypeEnum)2;
				val.Cell(0, 5).Text = mResourceManager.GetString("ManualThresholdString");
				val.Column(5).CellType = (CellTypeEnum)0;
				val.Column(5).Mask = (MaskEnum)6;
				val.Column(5).DecimalLength = 2;
				val.Cell(0, 6).Text = mResourceManager.GetString("OptimizationString");
				val.Column(6).CellType = (CellTypeEnum)1;
				val.ComboBox(6).Locked = true;
				val.ComboBox(6).Items.Add(mResourceManager.GetString("ManualString"));
				val.ComboBox(6).Items.Add(mResourceManager.GetString("AutoString"));
				val.ComboBox(6).Items.Add(mResourceManager.GetString("UnavailableString"));
				val.Cell(0, 7).Text = mResourceManager.GetString("AnalysisTypeString");
				val.Column(7).CellType = (CellTypeEnum)1;
				val.ComboBox(7).Locked = true;
				val.ComboBox(7).Items.Add(mResourceManager.GetString("QuantitativeString"));
				val.ComboBox(7).Items.Add(mResourceManager.GetString("QualitativeString"));
				val.Cell(0, 8).Text = mResourceManager.GetString("DigitalFilterString");
				val.Column(8).CellType = (CellTypeEnum)2;
				mAnalysisParameterConfigurationList = mPcrExerimentProject.AnalysisParameterConfigurationList;
				val.Rows = mPcrExerimentProject.TubeCount * mPcrExerimentProject.ProjectItems.Length + 1;
				for (int i = 1; i <= mAnalysisParameterConfigurationList.Count; i++)
				{
					IAbsoluteAnalysisParameterConfiguration absoluteAnalysisParameterConfiguration = mAnalysisParameterConfigurationList[i - 1] as IAbsoluteAnalysisParameterConfiguration;
					IChannelConfiguration channelConfiguration = mPcrExerimentProject.ChannelConfigurations[i - 1];
					if (!channelConfiguration.IsAvailable || !channelConfiguration.IsSelected)
					{
						val.Row(i).Visible = false;
						continue;
					}
					val.Cell(i, 1).Text = channelConfiguration.Content;
					val.Cell(i, 2).Text = absoluteAnalysisParameterConfiguration.StartBaseline.ToString();
					val.Cell(i, 3).Text = absoluteAnalysisParameterConfiguration.StopBaseline.ToString();
					val.Cell(i, 4).Text = absoluteAnalysisParameterConfiguration.IsAutoThreshold.ToString();
					val.Cell(i, 5).Text = absoluteAnalysisParameterConfiguration.ManualThreshold.ToString();
					val.Cell(i, 6).Text = absoluteAnalysisParameterConfiguration.GetBaselineOptimizationString();
					val.Cell(i, 7).Text = absoluteAnalysisParameterConfiguration.GetAnalysisTypeString();
					val.Cell(i, 8).Text = absoluteAnalysisParameterConfiguration.IsDigitalFilter.ToString();
					RefreshManualThresholdLocked(val, i);
					RefreshAutoThresholdLocked(val, i);
				}
				for (int j = 0; j < val.Cols; j++)
				{
					val.Column(j).Alignment = (AlignmentEnum)10;
					val.Column(j).AutoFit();
				}
				val.CellChange += new CellChangeEventHandler(optionGrid_CellChange);
				ultraTabPageControl2.Controls.Add((Control)(object)val);
				mIsOptionChanged.Add(mPcrExerimentProject.Name, false);
			}
			base.Controls.Add(mOptionsTabControl);
			mOptionsTabControl.BringToFront();
			panel1.SendToBack();
		}

		protected void optionGrid_CellChange(object Sender, CellChangeEventArgs e)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Expected O, but got Unknown
			Grid val = (Grid)Sender;
			if (e.Col == 4)
			{
				RefreshManualThresholdLocked(val, e.Row);
			}
			else if (e.Col == 7)
			{
				RefreshAutoThresholdLocked(val, e.Row);
			}
			mIsOptionChanged[(((Control)(object)val).Tag as IPcrExperimentProject).Name] = true;
		}

		protected void RefreshManualThresholdLocked(Grid grid, int rowIndex)
		{
			if (grid.Cell(rowIndex, 4).BooleanValue)
			{
				grid.Cell(rowIndex, 5).Locked = true;
				grid.Cell(rowIndex, 5).BackColor = Color.LightGray;
			}
			else
			{
				grid.Cell(rowIndex, 5).Locked = false;
				grid.Cell(rowIndex, 5).BackColor = Color.White;
			}
		}

		protected void RefreshAutoThresholdLocked(Grid grid, int rowIndex)
		{
			string text2 = grid.Cell(rowIndex, 7).Text;
			IAbsoluteAnalysisParameterConfiguration absoluteAnalysisParameterConfiguration = mPcrExerimentProjects[mOptionsTabControl.SelectedTab.Index].ProjectItems[rowIndex - 1].AnalysisParameterConfiguration as IAbsoluteAnalysisParameterConfiguration;
			if (absoluteAnalysisParameterConfiguration != null)
			{
				AbsoluteAnalysisType absoluteAnalysisType = absoluteAnalysisParameterConfiguration.AbsoluteAnalysisType;
				if (absoluteAnalysisType == AbsoluteAnalysisType.Qualitative)
				{
					grid.Cell(rowIndex, 4).Text = mResourceManager.GetString("FalseString");
					grid.Cell(rowIndex, 4).Locked = true;
					grid.Cell(rowIndex, 4).BackColor = Color.LightGray;
					RefreshManualThresholdLocked(grid, rowIndex);
				}
				else
				{
					grid.Cell(rowIndex, 4).Locked = false;
					grid.Cell(rowIndex, 4).BackColor = Color.White;
				}
			}
		}

		protected bool CheckEmptyOptionInGrid(Grid grid, int rowIndex)
		{
			for (int i = 1; i < grid.Cols; i++)
			{
				string text = grid.Cell(rowIndex, 1).Text;
				if (grid.Cell(rowIndex, i).Text.Length == 0)
				{
					MessageBox.Show(string.Concat(((Control)(object)grid).Tag, " ", text, " \"", grid.Cell(0, i).Text, "\" ", mResourceManager.GetString("CanNotBeEmpty")), mResourceManager.GetString("ErrorString"));
					return false;
				}
			}
			return true;
		}

		protected bool CheckBaseLineOptionInGrid(string channelName, Grid grid, int rowIndex, ref int startBaseLine, ref int stopBaseLine)
		{
			startBaseLine = grid.Cell(rowIndex, 2).IntegerValue;
			stopBaseLine = grid.Cell(rowIndex, 3).IntegerValue;
			if (startBaseLine <= 0)
			{
				MessageBox.Show(string.Concat(mResourceManager.GetString("ProjectString"), ((Control)(object)grid).Tag, ": ", mResourceManager.GetString("ChannelString"), channelName, " \"", grid.Cell(0, 2).Text, "\" ", mResourceManager.GetString("MustBeLargerThanZero"), "!"), mResourceManager.GetString("ErrorString"));
				return false;
			}
			if (startBaseLine >= stopBaseLine)
			{
				MessageBox.Show(string.Concat(mResourceManager.GetString("ProjectString"), ((Control)(object)grid).Tag, ": ", mResourceManager.GetString("ChannelString"), channelName, " \"", grid.Cell(0, 2).Text, "\" ", mResourceManager.GetString("MustBeLessThan"), "\"", grid.Cell(0, 3).Text, "\" !"), mResourceManager.GetString("ErrorString"));
				return false;
			}
			if (grid.Cell(rowIndex, 6).Text.CompareTo("Auto") == 0)
			{
				int num = (int)mConfigurationSerivce.AnalysisConfiguration.AutoOptimizationMinStartValue;
				int num2 = (int)mConfigurationSerivce.AnalysisConfiguration.AutoOptimizationMaxStopValue;
				if (startBaseLine < num)
				{
					MessageBox.Show(string.Concat(mResourceManager.GetString("InAutoOptimization"), mResourceManager.GetString("ProjectString"), ((Control)(object)grid).Tag, ": ", mResourceManager.GetString("ChannelString"), channelName, " \"", grid.Cell(0, 2).Text, "\" ", mResourceManager.GetString("MustBeLargerThan"), num.ToString(), "!"), mResourceManager.GetString("ErrorString"));
					return false;
				}
				if (stopBaseLine > num2)
				{
					MessageBox.Show(string.Concat(mResourceManager.GetString("InAutoOptimization"), mResourceManager.GetString("ProjectString"), ((Control)(object)grid).Tag, ":", mResourceManager.GetString("ChannelString"), channelName, " \"", grid.Cell(0, 3).Text, "\" ", mResourceManager.GetString("MustBeLessThan"), num2.ToString(), "!"), mResourceManager.GetString("ErrorString"));
					return false;
				}
			}
			else
			{
				try
				{
					IPcrExperimentProject pcrExperimentProject = ((Control)(object)grid).Tag as IPcrExperimentProject;
					if (stopBaseLine > pcrExperimentProject.PcrProgram.AmplificationDetectionCount && pcrExperimentProject.PcrProgram.AmplificationDetectionCount > 0)
					{
						MessageBox.Show(string.Concat(mResourceManager.GetString("InAutoOptimization"), mResourceManager.GetString("ProjectString"), ((Control)(object)grid).Tag, ":", mResourceManager.GetString("ChannelString"), channelName, " \"", grid.Cell(0, 3).Text, "\" ", mResourceManager.GetString("LessThanDetectionCycles"), "!"), mResourceManager.GetString("ErrorString"));
						return false;
					}
				}
				catch (Exception)
				{
				}
			}
			return true;
		}

		protected bool CheckManualThresholdInGrid(string channelName, Grid grid, int rowIndex, ref double manualThreshold)
		{
			manualThreshold = grid.Cell(rowIndex, 5).DoubleValue;
			if (manualThreshold <= 0.0)
			{
				MessageBox.Show(string.Concat(((Control)(object)grid).Tag, " ", channelName, " \"", grid.Cell(0, 5).Text, "\" ", mResourceManager.GetString("MustBeLargerThanZero")), mResourceManager.GetString("ErrorString"));
				return false;
			}
			return true;
		}

		protected void btnCancel_Click(object sender, EventArgs e)
		{
			Close();
		}

		protected void btnConfirm_Click(object sender, EventArgs e)
		{
			foreach (UltraTab tab in mOptionsTabControl.Tabs)
			{
				if (!tab.Visible || !tab.Enabled)
				{
					continue;
				}
				UltraTabPageControl tabPage = tab.TabPage;
				Control control = tabPage.Controls[0];
				Grid val = (Grid)(object)((control is Grid) ? control : null);
				if (!mIsOptionChanged[(((Control)(object)val).Tag as IPcrExperimentProject).Name])
				{
					continue;
				}
				for (int i = 1; i < val.Rows; i++)
				{
					if (val.Row(i).Visible)
					{
						if (!CheckEmptyOptionInGrid(val, i))
						{
							return;
						}
						string channelName = val.Cell(i, 1).Text;
						IAbsoluteAnalysisParameterConfiguration absoluteAnalysisParameterConfiguration = mAnalysisParameterConfigurationList[i - 1] as IAbsoluteAnalysisParameterConfiguration;
						int startBaseLine = 0;
						int stopBaseLine = 0;
						if (!CheckBaseLineOptionInGrid(channelName, val, i, ref startBaseLine, ref stopBaseLine))
						{
							return;
						}
						absoluteAnalysisParameterConfiguration.StartBaseline = startBaseLine;
						absoluteAnalysisParameterConfiguration.StopBaseline = stopBaseLine;
						absoluteAnalysisParameterConfiguration.IsAutoThreshold = val.Cell(i, 4).BooleanValue;
						double manualThreshold = 0.0;
						if (!CheckManualThresholdInGrid(channelName, val, i, ref manualThreshold))
						{
							return;
						}
						absoluteAnalysisParameterConfiguration.ManualThreshold = manualThreshold;
						absoluteAnalysisParameterConfiguration.SetBaselineOptimizationString(val.Cell(i, 6).Text);
						absoluteAnalysisParameterConfiguration.SetAnalysisTypeString(val.Cell(i, 7).Text);
						absoluteAnalysisParameterConfiguration.IsDigitalFilter = val.Cell(i, 8).BooleanValue;
						(absoluteAnalysisParameterConfiguration as AnalysisParameterConfiguration).IsNeedReAnalysis = true;
					}
				}
			}
			bool @checked = chkAutoAnalysis.Checked;
			Close();
		}
	}
}
